package com.atguigu.jxc.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.atguigu.jxc.dao.DamageListDao;
import com.atguigu.jxc.dao.DamageListGoodsDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.DamageList;
import com.atguigu.jxc.entity.DamageListGoods;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.DamageListGoodsService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 里
 * @Description:
 */
@Service
public class DamageListGoodsServiceImpl implements DamageListGoodsService {

    @Autowired
    private DamageListGoodsDao damageListGoodsDao;
    @Autowired
    private DamageListDao damageListDao;

    @Override
    public Map<String, Object> getDamageListGoodsList(String sTime, String eTime) {
        Map<String,Object> map = new HashMap<>();
        List<DamageList> damageListList = damageListDao.getDamageListGoodsList(sTime,eTime);
        if(!CollectionUtils.isEmpty(damageListList)){
            map.put("rows",damageListList);
        }
        return map;
    }

    @Override
    public ServiceVO saveDamageList(DamageList damageList, String damageListGoodsStr, HttpSession session) {
        User user = (User) session.getAttribute("currentUser");
        damageList.setUserId(user.getUserId());
        damageListDao.saveDamageList(damageList);
        List<DamageListGoods> damageListGoodsList = JSONObject.parseArray(damageListGoodsStr, DamageListGoods.class);
        if(!CollectionUtils.isEmpty(damageListGoodsList)){
            for (DamageListGoods damageListGoods : damageListGoodsList) {
                damageListGoods.setDamageListId(damageList.getDamageListId());
                damageListGoodsDao.saveDamageListGoods(damageListGoods);
            }
        }
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    @Override
    public Map<String, Object> goodsListById(Integer damageListId) {
        Map<String,Object> map = new HashMap<>();
        //根据报损单id查询报损商品的信息
        List<DamageListGoods> damageListGoodsList = damageListGoodsDao.goodsListById(damageListId);
        if(!CollectionUtils.isEmpty(damageListGoodsList)){
            map.put("rows",damageListGoodsList);
        }
        return map;
    }
}
